                  package com.yc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.yc.model.Dishesinfo;
import com.yc.model.Userinfo;
import com.yc.service.DishesService;
import com.yc.service.UserService;

@Controller
public class UserController
{
	@Autowired
	private UserService us;
	@Autowired
	private DishesService ds;
	

	@RequestMapping(value="/login")
	public String Login(HttpServletRequest req,String loginName,String password,Map map) {
		if (loginName == null || password == null)
		{
			return "pages/login";
	
		}
		Userinfo USER_INFO=us.loginUser(loginName, password);
		map.put("USER_INFO", USER_INFO);
		
		
		if (USER_INFO != null)
		{
			req.getSession().setAttribute("USER_INFO", USER_INFO);
			switch (USER_INFO.getRole())
			{
			case 1:
				return "pages/main/admin/main";
			case 2:
				return "pages/main/kitchen/kitchenmain";
			case 3:
				return "pages/main/waiters/paylist";
			}
		} else
		{
			return "pages/login";
		}
		return null;
	}
	@RequestMapping(value="/modifyuser.order")
	public String modifyUser(HttpServletRequest req,String userPass) {
		Userinfo uf = (Userinfo) req.getSession().getAttribute("USER_INFO");
		System.out.println(uf.getUserId()+"=========================================");
		Userinfo usf = new Userinfo();
		usf.setUserId(uf.getUserId());
		usf.setUserPass(userPass);
		System.out.println(userPass);
		us.modifyUserService(usf);
		
		
		return "pages/index";
		
	}
	@RequestMapping(value="/adduser.order")
	public String adduser(HttpServletRequest req,HttpServletResponse resp,Userinfo usm) throws IOException {	
		System.out.println(usm.getRole());
		System.out.println(usm.getUserAccount());
		System.out.println(usm.getUserPass());
		us.addUser(usm);
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.println(" <script type='text/javascript'> alert('用户添加成功，确定返回页面')</script>");
		out.println("</HTML>");
		out.flush();
		return  "pages/main/admin/useradmin";
		
	}
	@RequestMapping(value="/adddishes.order")
	public String addCaidan(HttpServletRequest req,HttpServletResponse resp,Dishesinfo dsf) throws IOException {	
		System.out.println(dsf);
		ds.addDishes(dsf);
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.println(" <script type='text/javascript'> alert('菜品添加成功，点击确定返回')</script>");
		out.println("</HTML>");
		out.flush();
		return  "pages/main/admin/dishesadmin";
		
	}
}













